/**
 * Created by wzm on 2017/9/5.
 */
$(function () {

    var nice = $("html").niceScroll();

    var skin = $.cookie("skin");
    changeSkin(skin);
    /*换肤*/
    $(".dropdown .changecolor li").click(function () {
        var skin = $(this).attr("id");
        changeSkin(skin);
        $.cookie('skin', skin, {expires: 7}); // 存储一个带7天期限的 cookie
    });

    /*左侧导航栏显示隐藏功能*/
    $(".sub-nav").click(function () {
        /*显示*/
        if ($(this).find("span:first").attr('class') == "title-icon fa fa-caret-right") {
            $(this).find("span:first").removeClass("fa-caret-right");
            $(this).find("span:first").addClass("fa-caret-down");
            $(this).parent("div").siblings().find("div").removeClass("sublist-up").addClass("sublist-down").find("span:first").removeClass("fa-caret-down").addClass("fa-caret-right");
            $(this).parent("div").siblings().find("div").next(".nav-content").hide(300);
            $(this).removeClass("sublist-down");
            $(this).addClass("sublist-up");
        }
        /*隐藏*/
        else {
            $(this).find("span:first").removeClass("fa-caret-down");
            $(this).find("span:first").addClass("fa-caret-right");
            $(this).removeClass("sublist-up");
            $(this).addClass("sublist-down");
        }
        // 修改数字控制速度， slideUp(500)控制卷起速度
        $(this).next(".nav-content").slideToggle(300).siblings(".nav-content").slideUp(300);
    });

    /*左侧导航栏缩进功能*/
    $(".left-main .sidebar-fold").click(function () {
        if ($(this).parent().attr('class') == "left-main left-full") {
            $(this).parent().removeClass("left-full");
            $(this).parent().addClass("left-off");
            $(this).parent().parent().find(".right-product").removeClass("right-full");
            $(this).parent().parent().find(".right-product").addClass("right-off");
            resizeTabContent();
            $('#tabs').css('margin-left','0');
            $(".right-full").css("left","48px");
        }
        else {
            $(this).parent().removeClass("left-off");
            $(this).parent().addClass("left-full");
            $(this).parent().parent().find(".right-product").removeClass("right-off");
            $(this).parent().parent().find(".right-product").addClass("right-full");
            resizeTabContent();
            $('#tabs').css('margin-left','0');
            $(".right-full").css("left","160px");

        }
    })
    /*左侧鼠标移入提示功能*/
    $(".sbox ul li").mouseenter(function () {
        if ($(this).find("span:last-child").css("display") == "none") {
            $(this).find("div").show();
        }
    }).mouseleave(function () {
        $(this).find("div").hide();
    });


    //绑定tabs的右键菜单
    $("#tabs").tabs({
        onContextMenu: function (e, title) {
            if (title != '首页') {
                e.preventDefault();
                $('#tabsMenu').menu('show', {
                    left: e.pageX,
                    top: e.pageY
                }).data("tabTitle", title);
            }
        }
    });
   //实例化menu的onClick事件
    $("#tabsMenu").menu({
        onClick: function (item) {
            closeTab(this, item.name);
        }
    });
    $(window).resize(function() {
       resizeTabContent();
    });

});


/**
 * tab内容自适应
 */
function resizeTabContent() {
    $('.tabs-header').css('width','100%');
    $('.tabs-panels').css('width','100%');
    $('.panel-body').css('width','100%');
    $('.tabs-wrap').css('width','100%');
}

/**
 * 切换皮肤
 * @param skin
 */
function changeSkin(skin) {
    if (skin != null) {
        $("link[skin!='']").each(function () {
            var s = $(this).attr("skin");
            if (s != 'undefined' && s != undefined) {
                $(this).attr("disabled", "disabled");
            }
        });
        $("link[skin='" + skin + "']").removeAttr("disabled");

        //改变frame的skin
        getAllFrames(window.frames);
        for (var i = 0; i < frameList.length; i++) {
            $(frameList[i].document).find('link[skin!=""]').each(function () {
                var s = $(this).attr("skin");
                if (s != 'undefined' && s != undefined) {
                    $(this).attr("disabled", "disabled");
                }
            });
            $(frameList[i].document).find("link[skin='" + skin + "']").removeAttr("disabled");
        }
    }
}

var frameList = [];
function _getAllFrames(frame) {
    frameList.push(frame);
    if (frame.frames.length !== 0) {
        getAllFrames(frame.frames);
    }
}

function getAllFrames(frames) {
    for (var i = 0; i < frames.length; i++) {
        _getAllFrames(frames[i]);
    }
}

//打开页签
var lineWidth = 0;
var time;
var index = 0;
function addTab(title, url) {
    index++;
    // 屏幕适配
    var ClientH = document.documentElement.clientHeight;
    var offsetH = ClientH-80;

    $('.panel').find('iframe').css('height', offsetH + 'px');
    $('#tabs').tabs('close', title);
    var content = '<iframe scrolling="auto"  id="iframe_tab_' + index + '"frameborder="0" allowTransparency="true"  src="' + url + '" style="width:100%;height:' + offsetH + 'px"></iframe>';
    $('#tabs').tabs('add', {
        title: title,
        content: content,
        closable: true
    });

    $('.panel').find('.panel-body').css('height', offsetH + 'px');
}

//关闭所有
function closeTab(menu, type) {
    var curTabTitle = $(menu).data("tabTitle");
    var tabs = $("#tabs");
    if (type === "close") {
        tabs.tabs("close", curTabTitle);
        return;
    }
    var allTabs = tabs.tabs("tabs");
    var closeTabsTitle = [];
    $.each(allTabs, function () {
        var opt = $(this).panel("options");
        if (opt.closable && type === "all") {
            closeTabsTitle.push(opt.title);
        }
    });
    for (var i = 0; i < closeTabsTitle.length; i++) {
        tabs.tabs("close", closeTabsTitle[i]);
    }
}
